/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.chenzehe.zdmya.action;

import static com.chenzehe.java.core.Status.STATUS_WAITING;
import static com.chenzehe.zdmya.Constants.SESSION_CHECKCODE;
import static com.chenzehe.zdmya.Constants.SESSION_USER;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.chenzehe.java.core.freemarker.HtmlPathHelp;
import com.chenzehe.java.core.utils.WebUtil;
import com.chenzehe.zdmya.domain.Baoliao;
import com.chenzehe.zdmya.domain.Category;
import com.chenzehe.zdmya.domain.Jy;
import com.chenzehe.zdmya.domain.User;
import com.chenzehe.zdmya.service.IBaoliaoService;
import com.chenzehe.zdmya.service.ICategoryService;
import com.chenzehe.zdmya.service.IJyService;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-7-20 下午11:15:36
 */
@Controller
public class BaoliaoAction extends BaseAction {
	@Resource
	IBaoliaoService baoliaoService;
	@Resource
	IJyService jyService;
	@Resource
	ICategoryService categoryService;

	@RequestMapping(value = "/baoliao.html")
	public String gobaoliao(HttpServletRequest request, ModelMap model) {
		return HtmlPathHelp.getHtmlPathFront("baoliao");
	}

	@RequestMapping(value = "/baoliao_jy.html")
	public String gobaoliaojy(HttpServletRequest request, ModelMap model) {
		List<Category> categorys = categoryService.queryByParentId(0);
		model.addAttribute("categorys", categorys);
		return HtmlPathHelp.getHtmlPathFront("baoliao_jy");
	}

	@RequestMapping(value = "/savebaoliao.html")
	@ResponseBody
	public String savebaoliao(@ModelAttribute("baoliao") Baoliao baoliao,
			HttpServletRequest request, ModelMap model) {
		if (StringUtils.isBlank(baoliao.getTitle())) {
			return "{\"result\":\"标题不能为空！\"}";
		}
		if (StringUtils.isBlank(baoliao.getUrl())) {
			return "{\"result\":\"链接不能为空！\"}";
		}
		if (StringUtils.isBlank(baoliao.getSummary())) {
			return "{\"result\":\"推荐理由不能为空！\"}";
		}
		String checkcode = WebUtil.getString(request, "checkcode");
		if (StringUtils.isBlank(checkcode)) {
			return "{\"result\":\"验证码不能为空!\"}";
		}
		String sesCheckCode = (String) WebUtils.getSessionAttribute(request,
				SESSION_CHECKCODE);
		if (!StringUtils.equalsIgnoreCase(sesCheckCode, checkcode)) {
			return "{\"result\":\"验证码不正确，请重新输入!\"}";
		}

		try {
			User user = (User) WebUtils.getSessionAttribute(request,
					SESSION_USER);
			if (user != null) {
				baoliao.setUserId(user.getId());
				baoliao.setUsername(user.getUsername());
			} else {
				baoliao.setUserId(0);
				baoliao.setUsername("");
			}
			baoliao.setIssueTime(new Date());
			baoliao.setStatus(STATUS_WAITING.getState());
			baoliao.setContentId(0);
			baoliao.setType(0);
			baoliaoService.saveBaoliao(baoliao);
		} catch (Exception e) {
			return "{\"result\":\"提交保存失败！\"}";
		}
		return "{\"result\":\"ok\"}";
	}

	@RequestMapping(value = "/savebaoliao_jy.html")
	@ResponseBody
	public String savebaoliaojy(HttpServletRequest request, ModelMap model) {
		String title = WebUtil.getString(request, "title");
		String content = WebUtil.getString(request, "content");
		String category = WebUtil.getString(request, "category");
		if (StringUtils.isBlank(title)) {
			return "{\"result\":\"标题不能为空！\"}";
		}
		if (StringUtils.isBlank(content)) {
			return "{\"result\":\"内容不能为空！\"}";
		}
		if (StringUtils.isBlank(category)) {
			return "{\"result\":\"类目不能为空！\"}";
		}
		String checkcode = WebUtil.getString(request, "checkcode");
		if (StringUtils.isBlank(checkcode)) {
			return "{\"result\":\"验证码不能为空!\"}";
		}
		String sesCheckCode = (String) WebUtils.getSessionAttribute(request,
				SESSION_CHECKCODE);
		if (!StringUtils.equalsIgnoreCase(sesCheckCode, checkcode)) {
			return "{\"result\":\"验证码不正确，请重新输入!\"}";
		}

		try {
			User user = (User) WebUtils.getSessionAttribute(request,
					SESSION_USER);
			Jy jy = new Jy();
			if (user != null) {
				jy.setUserId(user.getId());
				jy.setUserName(user.getUsername());
			} else {
				jy.setUserId(0);
				jy.setUserName("");
			}
			jy.setTitle(title);
			jy.setTitle2("");
			jy.setSummary("");
			jy.setContent(content);
			String[] categorys = category.split("\\|");
			jy.setCategoryParentEname(categorys[0]);
			jy.setCategoryParentName(categorys[1]);
			String[] area = WebUtil.getString(request, "area").split("/");
			jy.setAreaEname(area[0]);
			jy.setAreaName(area[1]);
			jy.setImg("");
			jy.setZhiCount(0);
			jy.setCommentCount(0);
			jy.setCollectionCount(0);
			jy.setIssueTime(new Date());
			jy.setStatus(STATUS_WAITING.getState());
			jyService.save(jy);
		} catch (Exception e) {
			return "{\"result\":\"提交保存失败！\"}";
		}
		return "{\"result\":\"ok\"}";
	}

}
